package com.example.demo.service.serviceImpl;

import com.example.demo.common.LogAnnotation;
import com.example.demo.mapper.WalletMapper;
import com.example.demo.pojo.Wallet;
import com.example.demo.service.WalletService;
import com.example.demo.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class WalletServiceImpl implements WalletService {
    @Autowired
    private WalletMapper walletMapper;
    @Override
    public Result get() {
        Long accountId = 1l;
        //假设钱包登录功能有一个拦截器做登录验证，用threadlocal保存用户信息
        // 先从theradLocal中获取当前登录用户id，假设id为1
        Wallet wallet=walletMapper.get(accountId);
        return Result.success(wallet.getMoney());
    }

    @Override
    public Result payHundred() {
        //先获取账户id
        Long accountId=1l;
        //根据账户id查询钱包
        Wallet wallet = walletMapper.get(accountId);
        //是否有100元钱
        if (wallet.getMoney() < 100) {
            return Result.fail(10001,"账户余额不足，请充值");
        }
        walletMapper.payHundred(wallet.getId());
        return Result.success("成功消费100元");
    }

    @Override
    public Result getTwenty() {
        //先获取账户id
        Long accountId=1l;
        //根据账户id查询钱包
        Wallet wallet = walletMapper.get(accountId);
        walletMapper.getTwenty(wallet.getId());
        return Result.success("退款成功");
    }
}
